// 订阅发布
const EventUtil = (function(){
    function createEvent(Event){
        let _eventList = {};

        let namespace = "default";

        var event = {
            _eventList,
            // 添加事件监听
            on(eventName,execute,alias){
                if(!_eventList[namespace]){
                    _eventList[namespace] = {};
                }
                if(!_eventList[namespace][eventName]){
                    _eventList[namespace][eventName] = [];
                }
                if(!alias){
                    alias = "default"
                }
                _eventList[namespace][eventName].push({
                    alias,
                    execute
                });
                // namespace = "default";
            },

            // 取消事件监听
            off(eventName,execute){
                if(!_eventList[namespace] || !_eventList[namespace][eventName]){
                    return;
                }
                for(let i = 0; i < _eventList[namespace][eventName].length; i++){
                    if(_eventList[namespace][eventName][i].execute == execute){
                        _eventList[namespace][eventName].splice(i--,1);
                        return true;
                    }
                }
                // namespace = "default";
            },

            // 通过别名取消事件
            aliasOff(eventName,alias){
                if(!_eventList[namespace] || !_eventList[namespace][eventName]){
                    return false;
                }
                for(let i = 0; i < _eventList[namespace][eventName].length; i++){
                    if(_eventList[namespace][eventName][i].alias == alias){
                        _eventList[namespace][eventName].splice(i--,1);
                        return true;
                    }
                }
            },

            // 命名空间
            useSpace(name){
                return {
                    on(eventName,fun){
                        namespace = name;
                        event.on(eventName,fun);
                        namespace = "default";
                    },
                    off(eventName,fun){
                        namespace = name;
                        event.off(eventName,fun);
                        namespace = "default";
                    },
                    aliasOff(eventName,alias){
                        namespace = name;
                        event.aliasOff(eventName,alias);
                        namespace = "default";
                    },
                    trigger(eventName,...args){
                        namespace = name;
                        event.trigger(eventName,...args);
                        namespace = "default";
                    }
                };
            },

            // 触发事件
            trigger(eventName,...args){
                if(!_eventList[namespace] || !_eventList[namespace][eventName]){
                    return;
                }
                _eventList[namespace][eventName].forEach(event=>event.execute.apply(this,args));
            }
        }

        if(Event){
            event.Event = Event;
        }

        return event;
    }

    let event = createEvent();

    function createChild(){
        let childEvent = createEvent(event);
        return childEvent;
    }

    event.createEvent = createChild;

    return event;
})();

// 获取属性的类型
function getRawType(value){
    return Object.prototype.toString.call(value).slice(8,-1);
}

function makeMap(str,isLowerCase){
    let map = {};
    let arr = str.split(",");
    arr.forEach(item=>{
        map[item] = true;
    });
    return isLowerCase?(str)=>map[str.toUpperCase()]:(str)=>map[str];
}

// 生成uuidv4
function uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

// 是否是个空对象
function isEmpty(obj){
    try{
        if(JSON.stringify(obj) === "{}" || !obj){
            return true;
        }
    }catch(e){
        return false;
    }
    return false;
}

// 节流器
function throttle(func,interval,bind){
    var saveArgus,
        saveThis,
        isThrottle = false;

    function worker(){
        if(isThrottle){
            saveArgus = arguments;
            saveThis = bind || this;
            return;
        }

        func.apply(this,arguments);
        
        isThrottle = true;

        setTimeout(()=>{
            isThrottle = false;
            if(saveArgus){
                worker.apply(saveThis,saveArgus);
                saveThis = saveArgus = null;
            }
        },interval || 16);
    }

    return worker;
}

// 防抖
function antiShake(fn,interval,self){
    let timer = 0;
    return function (...args){
        if(timer){
            clearTimeout(timer);
        }
        timer = setTimeout(()=>{
            fn.apply(self,args);
        },interval || 100);
    }
}

export {
    throttle,
    antiShake,
    isEmpty,
    uuidv4,
    makeMap,
    getRawType,
    EventUtil
}

